//
//  fourViewController.m
//  mac2
//
//  Created by 李劭华 on 2021/7/26.
//
#import "OutlineItemCellView.h"
#import "OutlineViewDataModel.h"

#import <Masonry/Masonry.h>

#import "fourViewController.h"
static NSString *OutlineItemCellViewIdentifier = @"OutlineItemCellViewIdentifier";
@interface fourViewController ()<NSOutlineViewDelegate,NSOutlineViewDataSource>
@property (nonatomic,strong) NSButton *firstBtn;
@property (nonatomic,strong) NSMutableArray *datArray;
@property (nonatomic,strong) NSOutlineView *outlineView;
@property (nonatomic,strong) NSScrollView *scrollView;
@property (nonatomic,strong) OutlineViewDataModel *dataModel;


@end

@implementation fourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    self.view.wantsLayer = YES;
    self.view.layer = [CALayer layer];
    self.view.layer.backgroundColor = [NSColor whiteColor].CGColor;
    self.view.frame = self.rect;
    [self initDataSource];
    [self initUI];
}

-(void)viewDidAppear
{
    [super viewDidAppear];
//    [self.outlineView expandItem:nil expandChildren:YES];
}
-(void)initDataSource{
    self.datArray=[NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i<20; i++) {
        [self.datArray addObject:[NSString stringWithFormat:@"当前数据----：%d",i]];
    }
    
    self.dataModel = [[OutlineViewDataModel alloc]init];
    self.dataModel.name=@"2021年";
    OutlineViewDataModel *level1Node = [[OutlineViewDataModel alloc]init];
    {
        level1Node.name = @"7月";
        
        OutlineViewDataModel *level1Node1 = [[OutlineViewDataModel alloc]init];
        level1Node1.name = @"1号";
        level1Node1.childNodes=@[];

        OutlineViewDataModel *level1Node2 = [[OutlineViewDataModel alloc]init];
        level1Node2.name = @"2号";
        level1Node2.childNodes=@[];
        OutlineViewDataModel *level1Node3 = [[OutlineViewDataModel alloc]init];
        level1Node3.name = @"3号";
        level1Node3.childNodes=@[];
        
        level1Node.childNodes =@[level1Node1,level1Node2,level1Node3];
    }
    
    OutlineViewDataModel *level2Node = [[OutlineViewDataModel alloc]init];
    {
        level2Node.name = @"8月";
        OutlineViewDataModel *level1Node1 = [[OutlineViewDataModel alloc]init];
        level1Node1.name = @"1号";
        level1Node1.childNodes=@[];

        OutlineViewDataModel *level1Node2 = [[OutlineViewDataModel alloc]init];
        level1Node2.name = @"2号";
        level1Node2.childNodes=@[];
        OutlineViewDataModel *level1Node3 = [[OutlineViewDataModel alloc]init];
        level1Node3.name = @"3号";
        level1Node3.childNodes=@[];
        
        level2Node.childNodes =@[level1Node1,level1Node2,level1Node3];
    }
    
    OutlineViewDataModel *level3Node = [[OutlineViewDataModel alloc]init];
    {
        level3Node.name = @"9月";
        OutlineViewDataModel *level1Node1 = [[OutlineViewDataModel alloc]init];
        level1Node1.name = @"1号";
        level1Node1.childNodes=@[];

        OutlineViewDataModel *level1Node2 = [[OutlineViewDataModel alloc]init];
        level1Node2.name = @"2号";
        level1Node2.childNodes=@[];
        OutlineViewDataModel *level1Node3 = [[OutlineViewDataModel alloc]init];
        level1Node3.name = @"3号";
        level1Node3.childNodes=@[];
        
        level3Node.childNodes =@[level1Node1,level1Node2,level1Node3];
    }
    self.dataModel.childNodes = @[level1Node,level2Node,level3Node];
}
-(void)initUI{
    [self.view addSubview:self.firstBtn];
    [self.firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(0);
        make.left.equalTo(self.view.mas_left).with.offset(15);
        make.width.height.mas_equalTo(100);
    }];
    
     [self.view addSubview:self.scrollView];
     
     [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(self.firstBtn.mas_bottom);
         make.left.right.bottom.equalTo(self.view);
     }];
    
     self.scrollView.contentView.documentView = self.outlineView;
     [self.outlineView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.edges.equalTo(self.scrollView.contentView);
         make.height.mas_equalTo(1000);
     }];
}
#pragma mark - 点击事件
-(void)touchBtn:(NSButton *)sender{
    NSLog(@"touchBtn---:%ld",(long)sender.tag);
    [self dismissViewController:self];

   
    
}
#pragma mark - 代理方法
-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    if (item!=nil && [item isKindOfClass:[OutlineViewDataModel class]]) {
        return ((OutlineViewDataModel *)item).childNodes.count;
    }
    return _dataModel.childNodes.count;
}
-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    if (!item) {
        return _dataModel.childNodes[index];
    }
    else
    {
        OutlineViewDataModel *model = item;
        return model.childNodes[index];
    }
}

-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if (!item) {
        return [_dataModel.childNodes count]>0;
    }
    else
    {
        OutlineViewDataModel *model = item;
        return model.childNodes.count>0;
    }
}


//-(NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
//{
//    OutlineItemCellView *cellView =[outlineView makeViewWithIdentifier:OutlineItemCellViewIdentifier owner:self];
//    if (!cellView) {
//        cellView = [[OutlineItemCellView alloc]init];
//        cellView.identifier = OutlineItemCellViewIdentifier;
//    }
//    return cellView;
//}
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    if ([item isKindOfClass:[OutlineViewDataModel class]]) {
        OutlineViewDataModel *model = item;
        return model.name;
    }
    else
    {
        return nil;
    }
    
}
//是否绘制组行样式
-(BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
    if (item && [item isKindOfClass:[OutlineViewDataModel class]] && ((OutlineViewDataModel *)item).childNodes) {
        return YES;
    }
    else{
        return NO;
    }
}

-(CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
{
    return 40;
}

#pragma mark - ui
-(NSButton *)firstBtn
{
    if (!_firstBtn) {
        _firstBtn = [NSButton buttonWithTitle:@"返回" target:self action:@selector(touchBtn:)];
//        _firstBtn.frame = CGRectMake(10, 20, 100, 40);
        _firstBtn.tag = 100;
    }
    return _firstBtn;
}

-(NSOutlineView *)outlineView{
    if (!_outlineView) {
        _outlineView = [[NSOutlineView alloc]initWithFrame:CGRectZero];
        _outlineView.delegate = self;
        _outlineView.dataSource = self;
        
        NSTableColumn *tableColumn = [[NSTableColumn alloc]init];
        tableColumn.resizingMask = NSTableColumnAutoresizingMask;
        tableColumn.title = self.dataModel.name;
        [_outlineView addTableColumn:tableColumn];
    }
    return _outlineView;
}
-(NSScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[NSScrollView alloc]initWithFrame:CGRectZero];
        _scrollView.hasVerticalScroller = YES;
        _scrollView.hasHorizontalScroller = YES;
    }
    return _scrollView;
}

@end
